/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */

function Application_Modules(callback)
{
	var xhr = CreateSplendidRequest('Rest.svc/GetModuleTable?TableName=MODULES&$orderby=MODULE_NAME asc', 'GET');
	xhr.onreadystatechange = function()
	{
		if ( xhr.readyState == 4 )
		{
			GetSplendidResult(xhr, function(result)
			{
				try
				{
					if ( result.status == 200 )
					{
						if ( result.d !== undefined )
						{
							MODULES = new Object();
							for ( var i = 0; i < result.d.results.length; i++ )
							{
								var sMODULE_NAME = result.d.results[i].MODULE_NAME;
								MODULES[sMODULE_NAME] = result.d.results[i];
							}
							//alert(dumpObj(MODULES, 'MODULES'));
							callback(1, MODULES);
						}
						else
						{
							callback(-1, xhr.responseText);
						}
					}
					else
					{
						if ( result.ExceptionDetail !== undefined )
							callback(-1, result.ExceptionDetail.Message);
						else
							callback(-1, xhr.responseText);
					}
				}
				catch(e)
				{
					callback(-1, SplendidError.FormatError(e, 'Application_Modules'));
				}
			});
		}
	}
	try
	{
		if ( MODULES == null )
			xhr.send();
		else
			callback(1, MODULES);
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'Application_Modules'));
	}
}

function Application_Config(callback)
{
	var xhr = CreateSplendidRequest('Rest.svc/GetModuleTable?TableName=CONFIG&$select=NAME,VALUE&$orderby=NAME asc', 'GET');
	xhr.onreadystatechange = function()
	{
		if ( xhr.readyState == 4 )
		{
			GetSplendidResult(xhr, function(result)
			{
				try
				{
					if ( result.status == 200 )
					{
						if ( result.d !== undefined )
						{
							CONFIG = new Object();
							for ( var i = 0; i < result.d.results.length; i++ )
							{
								var sNAME = result.d.results[i].NAME;
								CONFIG[sNAME] = result.d.results[i].VALUE;
							}
							//alert(dumpObj(CONFIG, 'CONFIG'));
							callback(1, CONFIG);
						}
						else
						{
							callback(-1, xhr.responseText);
						}
					}
					else
					{
						if ( result.ExceptionDetail !== undefined )
							callback(-1, result.ExceptionDetail.Message);
						else
							callback(-1, xhr.responseText);
					}
				}
				catch(e)
				{
					callback(-1, SplendidError.FormatError(e, 'Application_Config'));
				}
			});
		}
	}
	try
	{
		if ( CONFIG == null )
			xhr.send();
		else
			callback(1, CONFIG);
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'Application_Config'));
	}
}

function Application_Teams(callback)
{
	var xhr = CreateSplendidRequest('Rest.svc/GetModuleTable?TableName=TEAMS&$select=ID,NAME&$orderby=NAME asc', 'GET');
	xhr.onreadystatechange = function()
	{
		if ( xhr.readyState == 4 )
		{
			GetSplendidResult(xhr, function(result)
			{
				try
				{
					if ( result.status == 200 )
					{
						if ( result.d !== undefined )
						{
							TEAMS = new Object();
							for ( var i = 0; i < result.d.results.length; i++ )
							{
								var sID = result.d.results[i].ID;
								TEAMS[sID] = result.d.results[i];
							}
							//alert(dumpObj(TEAMS, 'TEAMS'));
							callback(1, TEAMS);
						}
						else
						{
							callback(-1, xhr.responseText);
						}
					}
					else
					{
						if ( result.ExceptionDetail !== undefined )
							callback(-1, result.ExceptionDetail.Message);
						else
							callback(-1, xhr.responseText);
					}
				}
				catch(e)
				{
					callback(-1, SplendidError.FormatError(e, 'Application_Teams'));
				}
			});
		}
	}
	try
	{
		// 01/01/2012 Paul.  Make this call more efficient by checking the enabled flag. 
		var b = SplendidCache.Config('enable_team_management');
		if ( b === undefined || b == null || b == false )
			b = false;
		else if ( b == 'true' || b == 'on' || b == '1' || b == true || b == 1 )
			b = true;
		
		if ( b )
		{
			if ( TEAMS == null )
				xhr.send();
			else
				callback(1, TEAMS);
		}
		else
		{
			TEAMS = new Object();
			callback(1, TEAMS);
		}
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'Application_Teams'));
	}
}


